package cn.lau.pp.module.vr.service.order;

import cn.lau.pp.framework.common.util.date.LocalDateTimeUtils;
import cn.lau.pp.module.vr.dal.dataobject.order.OrderDetailDO;
import cn.lau.pp.module.vr.dal.mysql.device.DeviceMapper;
import cn.lau.pp.module.vr.dal.mysql.game.GameMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.lau.pp.module.vr.controller.admin.order.vo.*;
import cn.lau.pp.module.vr.dal.dataobject.order.OrderDO;
import cn.lau.pp.framework.common.pojo.PageResult;
import cn.lau.pp.framework.common.pojo.PageParam;
import cn.lau.pp.framework.common.util.object.BeanUtils;

import cn.lau.pp.module.vr.dal.mysql.order.OrderMapper;

import static cn.lau.pp.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lau.pp.module.vr.enums.ErrorCodeConstants.*;

/**
 * vr订单 Service 实现类
 *
 * @author Lau
 */
@Service
@Validated
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private GameMapper gameMapper;

    @Resource
    private DeviceMapper deviceMapper;


    @Override
    public Integer createOrder(OrderSaveReqVO createReqVO) {
        // 插入
        OrderDO order = BeanUtils.toBean(createReqVO, OrderDO.class);
        orderMapper.insert(order);
        // 返回
        return order.getId();
    }

    @Override
    public void updateOrder(OrderSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderExists(updateReqVO.getId());
        // 更新
        OrderDO updateObj = BeanUtils.toBean(updateReqVO, OrderDO.class);
        orderMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrder(Integer id) {
        // 校验存在
        validateOrderExists(id);
        // 删除
        orderMapper.deleteById(id);
    }

    private void validateOrderExists(Integer id) {
        if (orderMapper.selectById(id) == null) {
            throw exception(ORDER_NOT_EXISTS);
        }
    }

    @Override
    public OrderDO getOrder(Integer id) {
        return orderMapper.selectById(id);
    }

    @Override
    public PageResult<OrderDetailDO> getOrderPage(OrderPageReqVO pageReqVO) {
        return orderMapper.selectPage(pageReqVO);
    }

    @Override
    public OrderAnalysisRespVO getOrderAnalysis() {
        // 获取当月数据
        OrderAnalysisTimeRespVO orderAnalysisMon = orderMapper.getOrderAnalysis(LocalDateTimeUtils.getMonth(), LocalDateTimeUtils.getMonthEnd());
        OrderAnalysisTimeRespVO orderAnalysisYear = orderMapper.getOrderAnalysis(LocalDateTimeUtils.getYear(), LocalDateTimeUtils.getYearEnd());
        OrderAnalysisRespVO respVO = new OrderAnalysisRespVO();
        respVO.setAmountMon(orderAnalysisMon.getAmount());
        respVO.setCountMon(orderAnalysisMon.getCount());
        respVO.setAmountYear(orderAnalysisYear.getAmount());
        respVO.setCountYear(orderAnalysisYear.getCount());
        return  respVO;
    }
}